/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import game.Card;
import game.Table;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.ViewScoped;
import org.icefaces.application.PushRenderer;
import other.Costants;

/**
 *
 * @author alessandro
 */
@ManagedBean
@ViewScoped
public class TableBean implements Serializable {

    boolean full;
    boolean tableExist;
    @ManagedProperty(value = "#{tablesManager}")
    private TablesManager tablesManager;
    @ManagedProperty(value = "#{chatBean}")
    private ChatBean chatBean;
    @ManagedProperty(value = "#{userManager}")
    private UserManager userManager;
    private String player1Name;
    private String player1Avatar;
    private String player1Cards;
    private String player1Table;
    private String player2Name;
    private String player2Avatar;
    private String player2Cards;
    private String player2Table;
    private String player3Name;
    private String player3Avatar;
    private String player3Cards;
    private String player3Table;
    private String player4Name;
    private String player4Avatar;
    private String player4Cards;
    private String player4Table;
    private String message;
    ArrayList<String> cardsPlayer;

    private boolean userIsPlaying;
    
    public TableBean() {
        PushRenderer.addCurrentView(Costants.PUSH_TABLE);
        player1Avatar = Costants.AVATAR_NONE;
        player2Avatar = Costants.AVATAR_NONE;
        player3Avatar = Costants.AVATAR_NONE;
        player4Avatar = Costants.AVATAR_NONE;
        player1Name = "-";
        player2Name = "-";
        player3Name = "-";
        player4Name = "-";
        player1Cards = Costants.ADDRESS_EMPTY;
        player2Cards = Costants.ADDRESS_EMPTY;
        player3Cards = Costants.ADDRESS_EMPTY;
        player4Cards = Costants.ADDRESS_EMPTY;
        cardsPlayer = new ArrayList<String>();
        //message = tablesManager.getTableByUser(userManager.getUsername()).getMessage();
        //userIsPlaying =  tablesManager.getTableByUser(userManager.getUsername()).isPlaying(userManager.getUsername());
    }

    public ChatBean getChatBean() {
        return chatBean;
    }

    public String getMessage() {
        message = tablesManager.getTableByUser(userManager.getUsername()).getMessage();
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public boolean isTableExist() {
        tableExist = tablesManager.tableExist(userManager.getUsername());
        if(!tableExist) {
            userManager.setState(Costants.CHATTING);
        }
        return tableExist;
    }

    public void setTableExist(boolean tableExist) {
        this.tableExist = tableExist;
    }

    public void setChatBean(ChatBean chatBean) {
        this.chatBean = chatBean;
    }

    public boolean isUserIsPlaying() {
        userIsPlaying =  tablesManager.getTableByUser(userManager.getUsername()).isPlaying(userManager.getUsername());
        return userIsPlaying;
    }

    public void setUserIsPlaying(boolean userIsPlaying) {
        this.userIsPlaying = userIsPlaying;
    }

    public String getPlayer1Avatar() {
        return player1Avatar;
    }

    public void setPlayer1Avatar(String player1Avatar) {
        this.player1Avatar = player1Avatar;
    }

    public String getPlayer1Name() {
        this.setPlayers();
        return player1Name;
    }

    public void setPlayer1Name(String player1Name) {
        this.player1Name = player1Name;
    }

    public String getPlayer2Avatar() {
        return player2Avatar;
    }

    public void setPlayer2Avatar(String player2Avatar) {
        this.player2Avatar = player2Avatar;
    }

    public String getPlayer2Name() {
        return player2Name;
    }

    public void setPlayer2Name(String player2Name) {
        this.player2Name = player2Name;
    }

    public String getPlayer3Avatar() {
        return player3Avatar;
    }

    public void setPlayer3Avatar(String player3Avatar) {
        this.player3Avatar = player3Avatar;
    }

    public String getPlayer3Name() {
        return player3Name;
    }

    public void setPlayer3Name(String player3Name) {
        this.player3Name = player3Name;
    }

    public String getPlayer4Avatar() {
        return player4Avatar;
    }

    public void setPlayer4Avatar(String player4Avatar) {
        this.player4Avatar = player4Avatar;
    }

    public String getPlayer4Name() {
        return player4Name;
    }

    public void setPlayer4Name(String player4Name) {
        this.player4Name = player4Name;
    }

    public String getPlayer1Table() {
        return player1Table;
    }

    public void setPlayer1Table(String player1Table) {
        this.player1Table = player1Table;
    }

    public String getPlayer2Table() {
        return player2Table;
    }

    public void setPlayer2Table(String player2Table) {
        this.player2Table = player2Table;
    }

    public String getPlayer3Table() {
        return player3Table;
    }

    public void setPlayer3Table(String player3Table) {
        this.player3Table = player3Table;
    }

    public String getPlayer4Table() {
        return player4Table;
    }

    public void setPlayer4Table(String player4Table) {
        this.player4Table = player4Table;
    }

    public ArrayList<String> getCardsPlayer() {
        return cardsPlayer;
    }

    public void setCardsPlayer(ArrayList<String> cardsPlayer) {
        this.cardsPlayer = cardsPlayer;
    }

    public UserManager getUserManager() {
        return userManager;
    }

    public void setUserManager(UserManager userManager) {
        this.userManager = userManager;
    }

    public boolean isFull() {
        full = false;
        try {
        full = tablesManager.getTableByUser(userManager.getUsername()).isFull();
        } catch (Exception e) {
            return false;
        }
        return full;
    }

    public void setFull(boolean full) {
        this.full = full;
    }

    public TablesManager getTablesManager() {
        return tablesManager;
    }

    public String getPlayer1Cards() {
        return player1Cards;
    }

    public void setPlayer1Cards(String player1Cards) {
        this.player1Cards = player1Cards;
    }

    public String getPlayer2Cards() {
        return player2Cards;
    }

    public void setPlayer2Cards(String player2Cards) {
        this.player2Cards = player2Cards;
    }

    public String getPlayer3Cards() {
        return player3Cards;
    }

    public void setPlayer3Cards(String player3Cards) {
        this.player3Cards = player3Cards;
    }

    public String getPlayer4Cards() {
        return player4Cards;
    }

    public void setPlayer4Cards(String player4Cards) {
        this.player4Cards = player4Cards;
    }

    public void setTablesManager(TablesManager tablesManager) {
        this.tablesManager = tablesManager;
    }



    public void setPlayers() {

        List<String> players = new ArrayList<String>();
        players.add("-");
        players.add("-");
        players.add("-");
        players.add("-");

        
        

        if(this.isTableExist()) {
            
            //setto i giocatori nell'array temporaneo
            Table table = tablesManager.getTableByUser(userManager.getUsername());
            int token = 5;
            if (table.isStarted()) {
                token = table.getDeck().getToken();
            }
            for (int i = 0; i < table.getPlayersList().size(); i++) {
                players.set(i, table.getPlayersList().get(i));
            }

            /*setto il giorcatore della view alla fine*/
            if (this.isUserIsPlaying()) {
                for (int i = 0; i < players.size(); i++) {
                    if (players.get(i).equals(userManager.getUsername())) {
                        players.remove(i);
                        players.add(userManager.getUsername());
                        //traduco il token per la view
                        if(token == i) {
                            token = 3;
                        } else if(token > i) {
                            token = token - 1;
                        }
                        break;
                    }
                }
                /* AGGIORNA CARTE */
                cardsPlayer.clear();
                ArrayList<Card> playerC = table.getCardsPlayer(userManager.getUsername());
                for (int c = 0; c < playerC.size(); c++) {
                    if (playerC.get(c).getState().equals(Costants.IN_DECK)) {
                        cardsPlayer.add(Costants.ADDRESS_CARD + playerC.get(c).getSeed() + playerC.get(c).getNumber() + Costants.PNG);
                    }
                }

                //BISOGNA AGGIORNARE LE CARTE DEL GIOCATORE DELLA VIEW
            }

            /* setto gli avatar e i nicknames */
            for (int i = 0; i < players.size(); i++) {
                //variabili di appoggio
                String tempAvatar = Costants.AVATAR_NONE;
                String tempCards = Costants.ADDRESS_EMPTY;
                String tempTable = Costants.ADDRESS_CARD_EMPTY;
                String name = "-";
                boolean flagStarted = true;
                name = players.get(i);
                Card tempCard = null;

                //setto lo stato nel caso il gioco non sia iniziato
                if (!(table.isStarted())) {
                    flagStarted = false;
                    tempCards = Costants.ADDRESS_WAITING;
                } 


                //setto nick e avatar
                if (name.contains(Costants.BOTNAME)) {
                    if (token == i) {
                        tempAvatar = Costants.AVATAR_BOT_PLAYING;
                    } else {
                        tempAvatar = Costants.AVATAR_BOT;
                    }

                } else if (name.equals(userManager.getUsername())) {
                    if (token == i) {
                        tempAvatar = Costants.AVATAR_PLAYER_PLAYING;
                    } else {
                        tempAvatar = Costants.AVATAR_PLAYER;
                    }
                } else if (name.equals("-")) {
                    tempAvatar = Costants.AVATAR_NONE;
                    tempCards = Costants.ADDRESS_EMPTY; //se non c'è nessuno lo stato è vuoto
                } else if (i == 0) {
                    if (token == i) {
                        tempAvatar = Costants.AVATAR_PROF_PLAYING;
                    } else {
                        tempAvatar = Costants.AVATAR_PROF;
                    }
                } else if (i == 1) {
                    if (token == i) {
                        tempAvatar = Costants.AVATAR_LEELA_PLAYING;
                    } else {
                        tempAvatar = Costants.AVATAR_LEELA;
                    }
                } else if (i == 2) {
                    if (token == i) {
                        tempAvatar = Costants.AVATAR_FRY_PLAYING;
                    } else {
                        tempAvatar = Costants.AVATAR_FRY;
                    }
                } else if (i == 3) {
                    if (token == i) {
                        tempAvatar = Costants.AVATAR_PLAYER_PLAYING;
                    } else {
                        tempAvatar = Costants.AVATAR_PLAYER;
                    }
                }


                //per ciascun giocatore setto i valori
                switch (i) {
                    case 0:
                        player1Avatar = tempAvatar;
                        player1Name = name;
                        if (flagStarted) {
                            int numberCards = table.getDeck().getCardsNumberInDeck(table.getDeck().getCardsPlayer1());
                            tempCards = Costants.ADDRESS_CARD_OTHERS + numberCards + Costants.PNG;

                            tempCard = table.getOnTableCard(table.getDeck().getCardsPlayer1());
                            if (tempCard != null) {
                                tempTable = tempCard.getSeed() + tempCard.getNumber();
                            }
                        }
                        player1Cards = tempCards;
                        player1Table = tempTable;
                        break;
                    case 1:
                        player2Avatar = tempAvatar;
                        player2Name = name;
                        if (flagStarted) {
                            int numberCards = table.getDeck().getCardsNumberInDeck(table.getDeck().getCardsPlayer2());
                            tempCards = Costants.ADDRESS_CARD_OTHERS + numberCards + Costants.PNG;

                            tempCard = table.getOnTableCard(table.getDeck().getCardsPlayer2());
                            if (tempCard != null) {
                                tempTable = tempCard.getSeed() + tempCard.getNumber();
                            }
                        }
                        player2Cards = tempCards;
                        player2Table = tempTable;
                        break;
                    case 2:
                        player3Avatar = tempAvatar;
                        player3Name = name;
                        if (flagStarted) {
                            int numberCards = table.getDeck().getCardsNumberInDeck(table.getDeck().getCardsPlayer3());
                            tempCards = Costants.ADDRESS_CARD_OTHERS + numberCards + Costants.PNG;

                            tempCard = table.getOnTableCard(table.getDeck().getCardsPlayer3());
                            if (tempCard != null) {
                                tempTable = tempCard.getSeed() + tempCard.getNumber();
                            }
                        }
                        player3Cards = tempCards;
                        player3Table = tempTable;
                        break;
                    case 3:
                        player4Avatar = tempAvatar;
                        player4Name = name;
                        if (flagStarted) {
                            int numberCards = table.getDeck().getCardsNumberInDeck(table.getDeck().getCardsPlayer4());
                            tempCards = Costants.ADDRESS_CARD_OTHERS + numberCards + Costants.PNG;

                            tempCard = table.getOnTableCard(table.getDeck().getCardsPlayer4());
                            if (tempCard != null) {
                                tempTable = tempCard.getSeed() + tempCard.getNumber();
                            }
                        }
                        player4Cards = tempCards;
                        player4Table = tempTable;
                        break;
                }



            }
        }
    }

    public void playOnTable() {
        String username = userManager.getUsername();
        if (this.isTableExist() && !this.isFull()) {
            tablesManager.addPlayer(username);
            userManager.setState(Costants.WAITING);
        }
        PushRenderer.render(Costants.PUSH_GROUP);
        PushRenderer.render(Costants.PUSH_TABLE);
    }

    public String leaveTable() {
        tablesManager.leaveTable(userManager.getUsername());
        PushRenderer.removeCurrentView(Costants.PUSH_TABLE);
        userManager.setState(Costants.CHATTING);
        PushRenderer.render(Costants.PUSH_GROUP);
        PushRenderer.render(Costants.PUSH_TABLE);
        return Costants.HOME;
    }

    public boolean isStarted() {
        return tablesManager.getTableByUser(userManager.getUsername()).isStarted();
    }

    public void play(String card) {
        String username = userManager.getUsername();
        Table table = tablesManager.getTableByUser(username);

        /*controlli*/

        table.play(username, card);
    }


}
